/*
 * Copyright (c) 2023, 2024,yzc.cn All rights reserved.
 *
 */
package com.by.server.impl;


import com.by.common.bean.PageBean;
import com.by.common.utils.AutoCode;
import com.by.common.utils.AutoData;
import com.by.common.utils.BytesUtils;
import com.by.mapper.PurApplicationDetailsMapper;
import com.by.mapper.PurApplicationMapper;
import com.by.mapper.PurAttachmentMapper;
import com.by.pojo.PurApplication;
import com.by.pojo.PurApplicationFollowup;

import com.by.server.PurApplicationServer;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;



import java.util.List;


/**
 * <p>Project: erp-cloud-api - PurApplicationServerImpl</p>
 * <p>Powered by yzc On 2024-01-09 11:41:23</p>
 * <p>描述：<p>
 *
 * @author yzc [yzc0415@126.com]
 * @version 1.0
 * @since 17
 */
@Service
public class PurApplicationServerImpl implements PurApplicationServer {

    @Autowired
    PurApplicationMapper purApplicationMapper;

    @Autowired
    PurApplicationDetailsMapper purApplicationDetailsMapper;

    @Autowired
    PurAttachmentMapper purAttachmentMapper;

    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 保存采购申请
     */
    @Override
    @Transactional
    public int insert(PurApplication purApplication) {
        String hanzi = hz(purApplication.getSourceType());
        String code = AutoCode.autoCodePrefix(hanzi)+ AutoData.getNowData();
        //查询最大的编码
        String maxCode = purApplicationDetailsMapper.MaxCode(code);
        //生产编码
        String newCode = AutoCode.getNewCode(maxCode,hanzi);

        //设置采购申请编号
        purApplication.setCode(newCode);
        //遍历采购申请明细表
        purApplication.getPurApplicationDetailsList().forEach(pur->{
            //进行采购申请表和采购明细表之间的一对多关系的关联
            pur.setApplicationCode(newCode);
            //保存采购明细数据
            purApplicationDetailsMapper.insert(pur);
        });
        //遍历附件表
        purApplication.getPurAttachmentList().forEach(att->{
            //进行采购申请表和采购附件表之间的一对多关系的关联
            att.setApplicationCode(newCode);
            //保存采购附件数据
            purAttachmentMapper.insert(att);
        });
        int rs = purApplicationMapper.insert(purApplication);
        if (rs > 0){
            //实例化采购跟进对线
            PurApplicationFollowup followup =new PurApplicationFollowup();
            //设置跟进的采购编码
            followup.setApplicationCode(newCode);
            //设置跟进的采购主题
            followup.setApplicationSubject(purApplication.getSubject());
            //设置跟进状态
            followup.setFollowStatus("采购中");
            //将对象转换成字节数组
            byte[] bytes = BytesUtils.getObjectBytes(followup);
            rabbitTemplate.send("followExchange","follow",new Message(bytes));
        }

        return rs;
    }

    /**
     * 采购申请列表
     */
    @Override
    public PageBean queryPageList(int page, int pageSize,String subject, String code, String begin, String end,
                                  Integer purchaseType, Double totalAmount, Integer inquireStatus, String pojoCode) {
        //1.设置分页参数
        PageHelper.startPage(page,pageSize);
        //2.执行条件查询
        List<PurApplication> purApplications = purApplicationMapper.queryPageList(subject, code, begin, end, purchaseType, totalAmount, inquireStatus, pojoCode);
        //3.格式化
        PageInfo pageInfo = new PageInfo<>(purApplications);
        //进行封装
        PageBean pageBean =new PageBean(page,pageSize,pageInfo.getTotal(),pageInfo.getPages(),purApplications);

        return pageBean;
    }

    /**
     * 采购申请明细表
     */
    @Override
    public PageBean queryList(int page, int pageSize, String pojoCode, String proName, String proCode, String supCode, Double totalAmount, Integer inquireStatus, Integer applicationStatus, String subject, String begin, String end, Integer purchaseType) {
        //1.设置分页参数
        PageHelper.startPage(page,pageSize);
        //2.执行条件查询
        List<PurApplication> list = purApplicationDetailsMapper.list(pojoCode, proName, proCode, supCode,
                totalAmount, inquireStatus, applicationStatus, subject, begin, end, purchaseType);
        //3.格式化
        PageInfo pageInfo = new PageInfo<>(list);
        //进行封装
        PageBean pageBean =new PageBean(page,pageSize,pageInfo.getTotal(),pageInfo.getPages(),list);
        return pageBean;
    }

    /**
     * 根据类型产生对应的编号
     * @param sourceType
     * @return
     */
    String hz(int sourceType){
        String code = "";
        switch (sourceType){
            case 1:
                code = "没有来源";
                break;
            case 2:
                code ="销售合同";
                break;
            case 3:
                code = "生产订单";
                break;
            case 4:
                code = "采购定价";
                break;
            default:
                code = "项目档案";
        }
        return code;
    }
}
